/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.base;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;

import com.orhanobut.logger.Logger;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.sdk.QbSdk;

import cn.jpush.android.api.JPushInterface;
import es.dmoral.toasty.Toasty;

import static common.conf.StaticConf.Tencent_CrashReportAppid;

/**
 * 优化后的程序初始化，一般在MainActivity运行
 * Created by JustTong on 2018/12/13.
 */
public class InitApplication {
    private Context mContext;
    private static InitApplication instances;

    public static InitApplication getInstances() {
        instances = new InitApplication();

        return instances;
    }

    public void init(Context context) {
        this.mContext = context;

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //极光推送集成SDK
                JPushInterface.setDebugMode(true);
                JPushInterface.init(mContext);
                //腾讯X5浏览服务，耗时300ms左右
                initQbSdk();
                //初始化BUGLY
                CrashReport.initCrashReport(mContext, Tencent_CrashReportAppid, false);
                //Toasty配置
                initToastyConfig();

            }
        }, 1000);//1秒后执行Runnable中的run方法


    }


    /**
     * Toasty配置
     */
    private void initToastyConfig() {
        Toasty.Config.getInstance()
//                .setErrorColor(ContextCompat.getColor(mContext, android.R.color.holo_red_light)) // optional
//                .setInfoColor(ContextCompat.getColor(mContext, android.R.color.black)) // optional
//                .setSuccessColor(ContextCompat.getColor(mContext, android.R.color.holo_green_dark)) // optional
//                .setWarningColor(ContextCompat.getColor(mContext, android.R.color.holo_orange_light)) // optional
//                .setTextColor(ContextCompat.getColor(mContext, android.R.color.white)) // optional
                .tintIcon(false) // optional (apply textColor also to the icon)
                .setToastTypeface(null) // optional
                .setTextSize(16) // optional
                .apply(); // required

//        Toasty.success(yourContext, "Success!", Toast.LENGTH_SHORT, true).show();
//        Toasty.Config.reset();
    }

    /**
     * QbSdk 内核接入
     */
    private void initQbSdk() {

        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                Logger.i("Tbs>>> onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                Logger.i("Tbs>>> onCoreInitFinished is OK");

            }
        };

        QbSdk.initX5Environment(mContext, cb);

    }
}
